﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.BLL;
using System.IO;
using SeDuce.DAL;
using SeDuce.Model;
using SeDuce.Utility;

public partial class Management_Common_Gallery : RepeaterPagerBase
{
    List<GalleryPic> pics = new List<GalleryPic>();
    List<int> picIDs = new List<int>();
    Lazy_Yu<GalleryBLL> lazyGalleryBLL = new Lazy_Yu<GalleryBLL>(() => new GalleryBLL());
    public GalleryBLL LazyGalleryBLL
    {
        get { return this.lazyGalleryBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegistrPager(this.Repeater1, this.rptPageFliper, 7, this.FillPageMoudle(20, SeDuce.DAL.GalleryDAL.sqlPics, new List<SortField>() { 
            new SortField() { FieldName = "ID", DESC = true }
        }));
    }
    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        List<string> file_list_to_delete = new List<string>();

        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkCheck");
            if (chk.Checked)
            {
                HiddenField hidPicID = (HiddenField)item.FindControl("hidPicID");
                HyperLink hlnkUrl = (HyperLink)item.FindControl("hlnkUrl");
                file_list_to_delete.Add(hlnkUrl.ToolTip);
                picIDs.Add(hidPicID.Value.ToInt());
            }
        }
        if (this.LazyGalleryBLL.DeletePics(picIDs))
        {
            foreach (string file in file_list_to_delete)
            {
                if (File.Exists(Server.MapPath(file)))
                {
                    File.Delete(Server.MapPath(file));
                }
            }
        }
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key7", "javascript:Init_SWFUpload();", true);
        this.BindPage(this.rptPageFliper.UniqueID, 1);
    }
    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key7", "javascript:Init_SWFUpload();", true);
    }
    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        if (Session["file_info"] != null)
        {
            List<string> file_list_to_delete = new List<string>();
            try
            {
                List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;

                string UploadPath = Server.MapPath(Misc.gallery_filepath);
                if (!System.IO.Directory.Exists(UploadPath))
                {
                    System.IO.Directory.CreateDirectory(UploadPath);
                }

                foreach (Thumbnail img in thumbnails)
                {
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Guid.NewGuid().ToString().Replace("-", "");
                    string fullfilename = filename + Path.GetExtension(img.ID).ToLower();
                    file_list_to_delete.Add(Misc.gallery_filepath + "/" + fullfilename);
                    FileStream fs = new FileStream(UploadPath + "\\" + fullfilename, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(img.Data);
                    bw.Close();
                    fs.Close();


                    pics.Add(new GalleryPic()
                    {
                        PicName = Misc.gallery_filepath + "/" + fullfilename,
                        Url = "http://www.seduce.com"
                    });
                }
                if (this.LazyGalleryBLL.InsertPics(pics))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
                }
                else
                {
                    throw new Exception("保存时发生错误");
                }
            }
            catch (Exception ex)
            {
                foreach (string file in file_list_to_delete)
                {
                    if (File.Exists(Server.MapPath(file)))
                    {
                        File.Delete(Server.MapPath(file));
                    }
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
            finally
            {
                Session.Remove("file_info");
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key7", "javascript:Init_SWFUpload();", true);
        }
    }

    protected void lbtnSaveEdit_Click(object sender, EventArgs e)
    {
        List<GalleryPic> picList = new List<GalleryPic>();
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            TextBox txtUrl = (TextBox)item.FindControl("txtUrl");
            HiddenField hidPicID = (HiddenField)item.FindControl("hidPicID");
            HyperLink hlnkUrl = (HyperLink)item.FindControl("hlnkUrl");
            picList.Add(new GalleryPic()
            {
                ID = hidPicID.Value.ToInt(),
                PicName = hlnkUrl.ToolTip,
                Url = txtUrl.Text.Trim()
            });
        }
        if (this.LazyGalleryBLL.UpdatePics(picList))
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
        }
    }
}